import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/http/request/room_classify_request.dart';
import 'package:elephant_fish/model/room_classify_entity.dart';
import 'package:elephant_fish/page/Index_history_play_page.dart';
import 'package:elephant_fish/page/index_hot_page.dart';
import 'package:elephant_fish/page/index_rank_page.dart';
import 'package:elephant_fish/page/index_search_page.dart';
import 'package:elephant_fish/page/index_skill_page.dart';
import 'package:elephant_fish/page/index_voice_home_page.dart';
import 'package:elephant_fish/page/login_page.dart';
import 'package:elephant_fish/util/TxUtils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class IndexHomePage extends StatefulWidget {
  const IndexHomePage({Key key}) : super(key: key);

  @override
  _IndexHotState createState() => _IndexHotState();
}

class _TabData {
  final Widget tab;
  final Widget body;
  _TabData({this.tab, this.body});
}

List<_TabData> _tabDataList = [];
var sum = 0;
class _IndexHotState extends State<IndexHomePage> {

  var tabBarList = _tabDataList.map((item) => item.tab).toList();
  var tabBarViewList = _tabDataList.map((item) => item.body).toList();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
   getInitDate();

  }
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: DefaultTabController(
        length: tabBarList.length,
          child: Scaffold(
            appBar: AppBar(
              automaticallyImplyLeading: false,
              actions: [
                InkWell(
                  child: Image.asset('assets/images/search1.png',width: 27,),
                  onTap: (){
                    Navigator.of(context).push( MaterialPageRoute(builder: (context)=>IndexSearchPage()));
                  },
                ),
                Container(
                  width: 14.5,
                ),
                InkWell(
                  child: Image.asset('assets/images/history.png',width: 27,),
                  onTap: (){
                    Navigator.of(context).push( MaterialPageRoute(builder: (context)=>IndexHistoryPlayPage()));
                  },
                ),
                Container(
                  width: 14.5,
                ),
                InkWell(
                  child: Image.asset('assets/images/winner.png',width: 27,),
                  onTap: (){
                    Navigator.of(context).push( MaterialPageRoute(builder: (context)=>IndexRankPage()));
                  },
                ),
                Container(
                  width: 11,
                ),
              ],
              backgroundColor: Color(0xff140c3c),
              title: Row(
                children: [
                  Expanded(child: TabBar(
                    // isScrollable: true, //可滚动
                    indicatorColor: Color(0xffff215d), //指示器的颜色
                    labelColor: Color(0xfff3f3f3), //选中文字颜色
                    unselectedLabelColor: Color(0xfff3f3f3), //为选中文字颜色
                    isScrollable: true,
                    unselectedLabelStyle: TextStyle(
                      fontSize: 15,
                    ),
                    labelStyle: TextStyle(
                      fontSize: 20,
                    ),
                    // indicatorSize: TabBarIndicatorSize.label, //指示器与文字等宽
                    tabs: tabBarList,
                  ),

                  ),

                ],
              ),
            ),
            body:
            TabBarView(
              children: tabBarViewList
            ),
          ),
      ),
    );
  }

  Future<void> getInitDate() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String appToken = prefs.getString('app-token');
    BaseRequest request = RoomClassifyRequest();
    request.add('type', 2).addHeader('usertoken',appToken);
    var result = await HiNet.getInstance().fire(request);
    if (result['code'] == -1) {
      TxUtils.showErrorToast('登录失效，请重新登录', context);
      Navigator.pop(context);
      Navigator.of(context).push(MaterialPageRoute(
        builder: (context) {
          return LoginPage(arguments: {'loginCode':'-1'},);
        },
      ));
    }else if(result['code'] == 0){
      List<Map<String,dynamic>> listMap = new List<Map<String, dynamic>>.from(result['data']);
      List<RoomClassifyEntity> list = List();
      for(int i = 0 ; i < listMap.length ; i ++){
        list.add(RoomClassifyEntity.fromJson(listMap[i]));
      }
      sum ++;
      for(int i = 0; i < list.length;i ++){
        if(sum == 1){
          if(list[i].name == '热门'){
            _tabDataList.add( _TabData(tab: Text(list[i].name), body: IndexHotPage(categoryName: list[i].id,list: list[i].children,)));
          }else if(list[i].name == '语音房'){
            _tabDataList.add(_TabData(tab: Text(list[i].name), body: IndexVoiceHomePage(categoryName: list[i].id,)));
          }else{
            _tabDataList.add(_TabData(tab: Text(list[i].name), body: IndexSkillPage(categoryName: list[i].id,)));
          }
        }
      }
      setState(() {
        tabBarList = _tabDataList.map((item) => item.tab).toList();
        tabBarViewList = _tabDataList.map((item) => item.body).toList();
      });
    }
  }
}
